<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['output:cjmOpen:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="type1"
          @click="handleUpdateCjm"
          v-hasPermi="['output:cjmOpen:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-circle-check"
          size="small"
          :disabled="(tjFlage = type1 || mxDate)"
          @click="handleSubMit"
          v-hasPermi="['output:cjmOpen:submit']"
        >
          提交
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="type1"
          @click="handleDelete"
          v-hasPermi="['output:cjmOpen:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['output:cjmOpen:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExportMx"
          v-hasPermi="['output:cjmOpen:mxExport']"
        >
          导出明细
        </el-button>
      </el-col>
      <Tag type="dot" size="default" color="#0fac09">提交</Tag>
      <Tag type="dot" size="default" color="#bb0331">通过</Tag>
      <Tag type="dot" size="default" color="#0323af">检验</Tag>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      :cell-style="iCellStyle"
      v-loading="loading"
      :data="cjmOpenList"
      :height="tableH"
      @row-click="clickRwm"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column
        label="借料单号"
        :show-overflow-tooltip="true"
        width="135"
        align="center"
        prop="cjmDjbh"
      />
      <el-table-column
        label="借料日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cjmRq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cjmRq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="借料仓库"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cjmCkdm"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.cjmCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="借料厂商"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cjmCsdm"
      >
        <template #default="scope">
          <dict-tag :options="allCsOptions" :value="scope.row.cjmCsdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cjmDesc"
      />
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cjmNgdesc"
      />
      <el-table-column
        label="开单人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cjmJlry"
      />
      <el-table-column
        label="开单日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cjmJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cjmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="批准人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cjmPzry"
      />
      <el-table-column
        label="批准日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cjmPzrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cjmPzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过账人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cjmGzry"
      />
      <el-table-column
        label="过账日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cjmGzrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cjmGzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            :disabled="scope.row.cjmTjflag == 1"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['output:cjmOpen:getCjd']"
          >
            添加
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-table
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :data="cjmChildrenList"
      :height="tableHChild"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" min-width="4%" align="center" />
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="cjdWldm"
      />
      <el-table-column
        label="规格描述"
        :show-overflow-tooltip="true"
        min-width="30%"
        prop="itmPmgg"
      />
      <el-table-column
        label="单位"
        min-width="10%"
        :show-overflow-tooltip="true"
        align="center"
        prop="itmDw"
      >
        <template #default="scope">
          <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
        </template>
      </el-table-column>
      <el-table-column
        label="存放位置"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="cjdCwdm"
      >
        <template #default="scope">
          <dict-tag :options="allCwOptions" :value="scope.row.cjdCwdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="最小包装"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="cjdMinbz"
      />
      <el-table-column
        label="包装数量"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="cjdBzsl"
      />
      <el-table-column
        label="申请数量"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="cjdSqsl"
      />
      <el-table-column
        label="出库数量"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="cjdCksl"
      />
      <el-table-column
        label="出库人员"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="cjdCkry"
      />
      <el-table-column
        label="出库日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cjdCkrq"
        min-width="10%"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cjmGzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        min-width="10%"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            :disabled="type1"
            @click="handleDele(scope.row)"
            v-hasPermi="['output:cjmOpen:deleteCjd']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      v-model="open"
      :fullscreen="true"
      :close-on-click-modal="false"
      append-to-body
    >
      <!-- 子项搜索条件框-->
      <el-form :model="queryChildParams" :inline="true">
        <el-row>
          <el-col :span="6">
            <el-form-item label="物料代码" prop="itmWldm">
              <el-input
                v-model="queryChildParams.itmWldm"
                placeholder="请输入物料代码"
                clearable
                size="small"
                @keyup.enter.native="serachChildrened"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规则描述" prop="itmPmgg">
              <el-input
                v-model="queryChildParams.itmPmgg"
                placeholder="请输入规则描述"
                clearable
                size="small"
                @keyup.enter.native="serachChildrened"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="display: flex; justify-content: flex-end">
              <el-button type="primary" @click="serachChildrened"
                >查询</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!--子项数据框-->
      <!--      @row-click="clickdel"-->
      <el-form>
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="serachAddList"
          border
          stripe
          :height="tableHAdd"
          ref="somCheckTable"
          @scrollBottom="load2"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            min-width="5%"
            type="index"
            align="center"
          />
          <el-table-column
            label="物料代码"
            min-width="20%"
            :show-overflow-tooltip="true"
            align="center"
            prop="cjdWldm"
          />
          <el-table-column
            label="规格描述"
            :show-overflow-tooltip="true"
            min-width="20%"
            prop="itmPmgg"
          />
          <el-table-column
            label="客户编码"
            :show-overflow-tooltip="true"
            min-width="10%"
            prop="cjdKhbm"
          />
          <el-table-column
            label="单位"
            min-width="10%"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="存放位置"
            min-width="12%"
            align="center"
            prop="cjdCwdm"
          >
            <template #default="scope">
              <dict-tag :options="allCwOptions" :value="scope.row.cjdCwdm" />
            </template>
          </el-table-column>
          <el-table-column
            label="最小包装"
            min-width="10%"
            :show-overflow-tooltip="true"
            align="center"
            prop="cjdMinbz"
          />
          <el-table-column
            label="已申请数"
            min-width="10%"
            :show-overflow-tooltip="true"
            align="center"
            prop="cjdSqsl"
          />
          <el-table-column
            label="可用库存"
            min-width="10%"
            :show-overflow-tooltip="true"
            align="center"
            prop="kcmKykc"
          />
          <el-table-column
            label="需申请数"
            min-width="10%"
            align="center"
            prop="cjdNeedSqsl"
          >
            <template #default="scope">
              <el-input
                class="inputName"
                @input="changValue(scope.row)"
                v-model="scope.row.cjdNeedSqsl"
              ></el-input>
            </template>
          </el-table-column>
        </el-table>
        <el-form-item
          style="margin-top: 25px; display: flex; justify-content: flex-end"
        >
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="handAddChildren">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="450px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="88px"
      >
        <el-col :span="24">
          <el-form-item label="借料单号" prop="cjmDjbh">
            <el-input
              v-model="queryParams.cjmDjbh"
              placeholder="请输入借料单号"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="建立日期">
            <el-date-picker
              v-model="daterangeCjmJlrq"
              :picker-options="pickerOptions"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :clearable="false"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态标志">
            <el-radio-group v-model="queryParams.cjmZtbz">
              <el-radio
                v-for="dict in cjmZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <!-- 添加或修改对外生产借料开单作业对话框 -->
    <el-dialog
      :title="title"
      v-model="opened"
      :close-on-click-modal="false"
      draggable
      width="680px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="12">
          <el-form-item label="借料单号" prop="cjmDjbh">
            <el-input
              v-model="form.cjmDjbh"
              disabled
              placeholder="请输入借料单号"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="借料日期" prop="cjmRq">
            <el-date-picker
              clearable
              size="small"
              v-model="form.cjmRq"
              type="date"
              value-format="yyyy-MM-dd"
              :clearable="false"
              placeholder="选择借料日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="借料仓库" prop="cjmCkdm">
            <el-select v-model="form.cjmCkdm" placeholder="请选择借料仓库">
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="借料厂商" prop="cjmCsdm">
            <el-select
              v-model="form.cjmCsdm"
              filterable
              placeholder="请选择借料厂商"
            >
              <el-option
                v-for="item in csOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="cjmDesc">
            <el-input
              v-model="form.cjmDesc"
              type="textarea"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCjmOpen,
  getCjmOpen,
  delCjmOpen,
  addCjmOpen,
  updateCjmOpen,
  exportCjmOpen,
  childrenList,
  jlAddList,
  sumitChildren,
  updateCjmTj,
  deleCjm,
  exportCjmMx,
} from "@/api/output/cjmOpen";
import {
  getAllCs,
  getCs,
  listPutStorageAdds,
} from "@/api/putStorage/putStorageAdd";
import { getAll, getstorageType } from "@/api/repertory/Storage";
import { listRwdDet } from "@/api/putStorage/RwdDet";
import { insRtdDet, updateProMat } from "@/api/putStorage/proMatStart";

export default {
  name: "CjmOpen",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      tjFlage: false,
      mxDate: true,
      type1: true,
      // 选中数组
      ids: [],
      unitFlagOptions: [],
      allCwOptions: [],
      tableH: 250,
      tableHChild: 300,
      tableHAdd: 250,
      tableHAddChild: 300,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      total2: 0,
      rowData: null,
      // 对外生产借料开单作业表格数据
      cjmOpenList: [],
      cjmChildrenList: [],
      //存储厂商
      csOptions: [],

      allCsOptions: [],

      //有效存储仓库
      storageOptions: [],
      serachAddList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opened: false,
      selectioned: [],
      // 查询子项参数
      queryChildParams: {
        pageNum: 1,
        pageSize: 25,
        //仓库代码
        cjmCkdm: null,
        //订单编号
        cjmDjbh: null,
        itmPmgg: null,
        itmWldm: null,
      },
      // 状态标志字典
      cjmZtbzOptions: [],
      allStorageOptions: [],
      // // 建立日期时间范围
      // daterangeCjmJlrq: [],
      // 建立日期时间范围
      daterangeCjmJlrq: [
        this.$moment().startOf("month").toDate(),
        this.$moment().add(100, "year").endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cjmDjbh: null,
        cjmZtbz: "1",
        cjmJlrq: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cjmRq: [
          { required: true, message: "借料日期不能为空", trigger: "blur" },
        ],
        cjmCkdm: [
          { required: true, message: "仓库代码不能为空", trigger: "change" },
        ],
        cjmCsdm: [
          { required: true, message: "厂商代码不能为空", trigger: "change" },
        ],
      },
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_cjm_ztbz").then((response) => {
      this.cjmZtbzOptions = response.data;
    });
    getAll().then((response) => {
      this.allStorageOptions = response.data;

      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    getstorageType("0").then((response) => {
      this.storageOptions = response.data;

      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    getAll().then((response) => {
      this.allCwOptions = response.data;

      for (let item2 of this.allCwOptions) {
        item2.dictLabel = item2.storageDefaultstorage;

        item2.listClass = "default";
        item2.dictValue = item2.storageId + "";
      }
    });
    getCs().then((response) => {
      this.csOptions = response.data;
      for (let item of this.csOptions) {
        item.dictLabel = item.csmCs;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });
    getAllCs().then((response) => {
      this.allCsOptions = response.data;
      for (let item of this.allCsOptions) {
        item.dictLabel = item.csmCs;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });
  },
  methods: {
    /** 颜色事件 */
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //提交
      if (row.cjmZtbz == "1" && row.cjmTjflag == "1") {
        return "color:#0fac09";
      }
      //通过
      else if (row.cjmZtbz == "2" && row.cjmTjflag == "1") {
        return "color:#0323af";
      }
      //检验
      else if (row.cjmZtbz == "6" && row.cjmTjflag == "1") {
        return "color:#bb0331";
      }
    },
    /** 查询对外生产借料开单作业列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCjmJlrq && "" != this.daterangeCjmJlrq) {
        this.queryParams.params["beginCjmJlrq"] = this.moment(
          this.daterangeCjmJlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endCjmJlrq"] = this.moment(
          this.daterangeCjmJlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listCjmOpen(this.queryParams).then((response) => {
        this.cjmOpenList = [];
        this.cjmChildrenList = [];
        this.cjmOpenList = this.cjmOpenList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickRwm(row);
        } else if (this.cjmOpenList != null && this.cjmOpenList.length > 0) {
          this.clickRwm(this.cjmOpenList[0]);
        } else {
          this.type1 = true;
        }
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 25;
        this.queryParams.pageNum++;
        listCjmOpen(this.queryParams).then((response) => {
          this.cjmOpenList = this.cjmOpenList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    load2() {
      if (
        this.queryChildParams.pageNum <
        Math.ceil(this.total2 / this.queryChildParams.pageSize)
      ) {
        this.queryChildParams.pageSize = 25;
        this.queryChildParams.pageNum++;
        jlAddList(this.queryChildParams).then((response) => {
          this.serachAddList = this.serachAddList.concat(response.rows);
          this.total2 = response.total;
        });
      }
      return 0;
    },
    // /** 确认添加提交物料*/
    // searchAddRtd(){
    //   if (this.checkSerachList.length >0){
    //     for (let i of this.checkSerachList){
    //       if (i.rtdSqsl ==null || i.rtdSqsl <= 0){
    //         this.msgError("物料: < "+i.itmPmgg+" > 请输入合法数量~")
    //         return;
    //       }else {
    //         i.rtdDjbh = this.rtmDjbhs;
    //         i.rtdCwdm  = i.rtdCwdm
    //       }
    //     }
    //   }else {
    //     this.msgError("请勾选需要添加的物料~")
    //     return;
    //   }
    //   insRtdDet(this.checkSerachList).then(res =>{
    //     this.msgSuccess("添加成功~")
    //   }).catch(() =>{
    //     this.msgError("添加失败~")
    //   });
    //   this.queryChildParams.pageNum = 1
    //   this.getSerachChild()
    //   //回显字表
    //   this.getListRtdDet(this.listData);
    // },
    serachChildrened() {
      this.queryChildParams.pageNum = 1;
      jlAddList(this.queryChildParams).then((response) => {
        this.serachAddList = [];
        this.serachAddList = this.serachAddList.concat(response.rows);
        this.total2 = response.total;
      });
      // this.getRwdDetList(this.rowData.rwmDjbh)
    },
    /** 提交操作按钮 */
    handleSubMit() {
      // const  data = {'rtmId':this.ids,'rtmZtbz':1,'rtmTjflag':1}
      updateCjmTj(this.queryChildParams.cjmDjbh).then((res) => {
        this.msgSuccess("提交成功~");
        // this.check_submit  = false;
        this.queryParams.pageNum = 1;
        this.cjmOpenList = [];
        this.getList();
      });
    },
    //删除
    handleDele(row) {
      const wldm = row.cjdWldm;
      deleCjm(this.queryChildParams.cjmDjbh, wldm).then((res) => {
        this.msgSuccess("删除成功~");
        this.getRwdDetList(this.queryChildParams.cjmDjbh);
      });
    },
    //添加
    handleUpdate(row) {
      this.reset();
      this.queryChildParams.cjmDjbh = row.cjmDjbh;
      this.queryChildParams.cjmCkdm = row.cjmCkdm;
      this.queryChildParams.pageNum = 1;
      jlAddList(this.queryChildParams).then((response) => {
        this.serachAddList = [];
        this.serachAddList = response.rows;
        this.total2 = response.total;
      });
      this.open = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.opened = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cjmId: null,
        cjmDjbh: null,
        cjmRq: new Date(),
        cjmCkdm: null,
        cjmCsdm: null,
        cjmDesc: null,
        cjmTjflag: null,
        cjmNgdesc: null,
        cjmZtbz: "1",
        cjmJlry: null,
        cjmJlrq: null,
        cjmPzry: null,
        cjmPzrq: null,
        cjmGzry: null,
        cjmGzrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.cjmOpenList = [];
      this.getList();
      this.openSearch = false;
    },
    changValue(row) {
      if (row.cjdNeedSqsl > row.kcmKykc) {
        this.msgError("不允许超过可用库存");

        row.cjdNeedSqsl = row.kcmKykc;
      }
    },
    handAddChildren() {
      const datas = this.serachAddList.filter((a) => {
        return !!a.cjdNeedSqsl && a.cjdNeedSqsl > 0;
      });
      if (datas.length < 1) {
        this.msgError("请输入需要添加的物料~");
        return;
      }
      sumitChildren(datas)
        .then((response) => {
          this.msgSuccess("添加成功~");
          this.queryChildParams.pageNum = 1;
          this.serachAddList = [];
          this.serachChildrened();
          this.getRwdDetList(this.queryChildParams.cjmDjbh);
        })
        .catch(() => {
          this.msgError("添加失败~");
        });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCjmJlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    clickRwm(row) {
      // //获得选中的详细数据
      this.rowData = row;
      // //获得当前单据的状态
      this.ids = row.cjmId;
      this.type1 = !(row != null && row.cjmZtbz == "1" && row.cjmTjflag == "0");
      if (row != null) {
        // this.cjmZtbz = row.cjmZtbz
      }
      this.queryChildParams.cjmDjbh = row.cjmDjbh;
      //获得选中的详细数据
      this.getRwdDetList(row.cjmDjbh);
    },
    /** 获取RwdDet 数据*/
    getRwdDetList(cjmDjbh) {
      childrenList(cjmDjbh).then((response) => {
        this.cjmChildrenList = response.data;
        this.mxDate = this.cjmChildrenList.length > 0 ? false : true;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.opened = true;
      this.title = "添加对外生产借料开单作业";
    },
    handleUpdateCjm(row) {
      this.reset();
      const cjmId = row.cjmId || this.rowData.cjmId;
      getCjmOpen(cjmId).then((response) => {
        this.form = response.data;
        this.opened = true;
        this.title = "修改对外生产借料开单作业";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询对外生产借料开单作业";
      this.openSearch = true;
    },

    handBindVal(row) {
      if (row.cjdNeedSqsl) {
        if (row.cjdBzsl != null && row.cjdBzsl != 0) {
          return row.cjdNeedSqsl;
        } else {
          return 0;
        }
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.cjmRq = this.$moment(this.form.cjmRq).format();
          if (this.form.cjmId != null) {
            updateCjmOpen(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.opened = false;
              this.getList(this.rowData);
            });
          } else {
            addCjmOpen(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.opened = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cjmIds = row.cjmId || this.ids;
      this.$confirm(
        '是否确认删除对外生产借料开单作业编号为"' +
          this.rowData.cjmDjbh +
          '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delCjmOpen(cjmIds);
        })
        .then(() => {
          this.queryParams.pageNum = 1;
          this.cjmOpenList = [];
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有对外生产借料开单作业数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportCjmOpen(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },

    /** 导出明细按钮操作 */
    handleExportMx() {
      const queryParams = this.rowData.cjmDjbh;
      this.$confirm("是否确认导出所有对外生产借料开单作业明细数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportCjmMx(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
